const qiniuUploader = require("./qiniuUploader-min");
var http = require('./http');
var api = require('./api');

function upload(path,fun) {
  return new Promise((resolve, reject) => {
    qiniuUploader.upload(path, (res) => {
        // resolve(wx.getStorageSync('bucketName') + res.imageURL)
        resolve(wx.getStorageSync('bucketName').slice(0,-1) + res.imageURL)
      }, (error) => {
        console.error('error: ' + JSON.stringify(error));
      },
      null,
      (progress) => {
        fun(progress.progress)
      },cancelTask => {}
    );
  })
}

function getNowTime() {
  let dateTime
  let yy = new Date().getFullYear()
  let mm = new Date().getMonth() + 1
  let dd = new Date().getDate()
  let hh = new Date().getHours()
  let mf = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() :
    new Date().getMinutes()
  let ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() :
    new Date().getSeconds()
  dateTime = yy + '-' + mm + '-' + dd
  return dateTime
}

function mathRand() {
  var Num = "";
  for (var i = 0; i < 6; i++) {
    Num += Math.floor(Math.random() * 10);
  }
  return getNowTime() + ' ' + Num
}

function addPicResource(data) {
  return new Promise((resolve, reject) => {
    http.postHttp(api.getResourceDetail, data,
      (res) => {
        if (res.code == "A00000") {
          resolve()
        } else {
          wx.showModal({
            showCancel:false,
            content: res.message,
          })
        }
      },
      (err) => {
        console.log(err);
      },
    )
  })
}

function addVideoResource(data) {
  return new Promise((resolve, reject) => {
    http.postHttp(api.getResourceDetail, data,
      (res) => {
        if (res.code == "A00000") {
          resolve()
        } else {
          wx.showModal({
            showCancel:false,
            content: res.message,
          })
        }
      },
      (err) => {
        console.log(err);
      },
    )
  })
}
module.exports = {
  upload,
  mathRand,
  addPicResource,
  addVideoResource
}